﻿using PacoToolKit.Format;
using Protocol.OUSProtocol.Model;
using System.Collections.Generic;

namespace Protocol.OUSProtocol.Decoder
{
    public class BaseDecoder
    {
        public static Status DecodeStatus(string data)
        {
            Status status = new Status();

            //S16 实际温度 3路
            List<string> temp_r_List = new List<string>();
            string temp_r = Formater.TwoByteHEXtoDEC(Formater.ReverseHexString(data.Substring(0, 2 * 2)));//2字节
            temp_r = (decimal.Parse(temp_r) / 10).ToString("#0.0");
            if (decimal.Parse(temp_r) < -30 || decimal.Parse(temp_r) > 60)
            {
                temp_r = "-";
            }
            temp_r_List.Add(temp_r);

            temp_r = Formater.TwoByteHEXtoDEC(Formater.ReverseHexString(data.Substring(4, 2 * 2)));//2字节
            temp_r = (decimal.Parse(temp_r) / 10).ToString("#0.0");
            if (decimal.Parse(temp_r) < -30 || decimal.Parse(temp_r) > 60)
            {
                temp_r = "-";
            }
            temp_r_List.Add(temp_r);

            temp_r = Formater.TwoByteHEXtoDEC(Formater.ReverseHexString(data.Substring(8, 2 * 2)));//2字节
            temp_r = (decimal.Parse(temp_r) / 10).ToString("#0.0");
            if (decimal.Parse(temp_r) < -30 || decimal.Parse(temp_r) > 60)
            {
                temp_r = "-";
            }
            temp_r_List.Add(temp_r);

            status.temp_r_List = temp_r_List;

            //U16 humi_r[NHumi];      //实际湿度 1路
            string humi_r = Formater.ReverseHexString(data.Substring(12, 2 * 2)); //2字节
            humi_r = (decimal.Parse(Formater.HEXtoDEC(humi_r)) / 10).ToString("#0.0");
            if (decimal.Parse(humi_r) < 0 || decimal.Parse(humi_r) > 99)
            {
                humi_r = "-";
            }
            status.humi_r = humi_r;

            //U16 nh3_r;              //氨气
            string nh3_r = Formater.ReverseHexString(data.Substring(16, 2 * 2)); //2字节
            nh3_r = Formater.HEXtoDEC(nh3_r);
            if (decimal.Parse(nh3_r) < 0 || decimal.Parse(nh3_r) > 1000)
            {
                nh3_r = "-";
            }
            status.nh3_r = nh3_r;

            //U16 co2_r;              //CO2
            string co2_r = Formater.ReverseHexString(data.Substring(20, 2 * 2)); //2字节
            co2_r = Formater.HEXtoDEC(co2_r);
            if (decimal.Parse(co2_r) < 0 || decimal.Parse(co2_r) > 5000)
            {
                co2_r = "-";
            }
            status.co2_r = co2_r;

            //U32 wind_r;             //通风量
            string wind_r = Formater.ReverseHexString(data.Substring(24, 4 * 2)); //4字节
            status.wind_r = (int.Parse(Formater.HEXtoDEC(wind_r))).ToString();

            //U16 temp_avg;           //室内平均温度
            string temp_avg = Formater.ReverseHexString(data.Substring(32, 2 * 2)); //2字节
            temp_avg = (decimal.Parse(temp_avg) / 10).ToString("#0.0");
            if (decimal.Parse(temp_avg) < -30 || decimal.Parse(temp_avg) > 60)
            {
                temp_avg = "-";
            }
            status.temp_avg = temp_avg;

            //U16 temp_target;        //目标温度
            string temp_target = Formater.ReverseHexString(data.Substring(36, 2 * 2)); //2字节
            status.temp_target = (decimal.Parse(Formater.HEXtoDEC(temp_target)) / 10).ToString("#0.0");

            //U16 day;                //日龄
            string day = Formater.ReverseHexString(data.Substring(40, 2 * 2)); //2字节
            status.day = Formater.HEXtoDEC(day);

            //U8 posFan;               //当前风机百分比
            string posFan = data.Substring(44, 2 * 1); //1字节
            status.posFan = Formater.HEXtoDEC(posFan);

            //U8 posDoor;             //风门位置
            string posDoor = data.Substring(46, 2 * 1); //1字节
            status.posDoor = Formater.HEXtoDEC(posDoor);

            //U8 modeTemp;             //温度控制模式 0-自设 1-曲线
            string modeTemp = data.Substring(48, 2 * 1); //1字节
            byte value = byte.Parse(Formater.HEXtoDEC(modeTemp));
            status.modeTemp = (value == 1 ? "曲线" : "自设");

            //U8 modeRun;              //运行模式 0-空舍 1-生产
            string modeRun = data.Substring(50, 2 * 1); //1字节
            value = byte.Parse(Formater.HEXtoDEC(modeRun));
            status.modeRun = (value == 1 ? "生产" : "空舍");

            //U8 modeCtrl;             //控制模式 0-线性 1-比例 2-固定
            string modeCtrl = data.Substring(52, 2 * 1); //1字节
            value = byte.Parse(Formater.HEXtoDEC(modeRun));
            if (value == 0)
            {
                status.modeCtrl = "线性";
            }
            else if (value == 1)
            {
                status.modeCtrl = "比例";
            }
            else if (value == 2)
            {
                status.modeCtrl = "固定";
            }


            //U8 modeDoor;            //风门模式 0=变频, 1=继电器, 3=无
            string modeDoor = data.Substring(54, 2 * 1); //1字节
            value = byte.Parse(Formater.HEXtoDEC(modeRun));
            if (value == 0)
            {
                status.modeDoor = "变频";
            }
            else if (value == 1)
            {
                status.modeDoor = "继电器";
            }
            else if (value == 3)
            {
                status.modeDoor = "无";
            }

            //U32 alarmState;         //报警状态，每1bit 表示一种报警 ,该位为0表示无此报警
            string alarmState = Formater.ReverseHexString(data.Substring(56, 2 * 4));//4字节
            status.alarmState = Formater.HEXtoDEC(alarmState);

            //U32 relayState;         //每种类型的继电器对应的开关状态 
            string relayState = Formater.ReverseHexString(data.Substring(64, 2 * 4));//4字节
            status.relayState = Formater.HEXtoDEC(relayState);

            //U8  version[16];        //版本号
            string version = Formater.ASCLLHexBytesToString(Formater.HexStringToBytesKeep(data.Substring(72, 2 * 16)));//4字节
            status.version = version;

            //U32 CPUID;              //CPUID
            string CPUID = Formater.ReverseHexString(data.Substring(104, 2 * 4));//4字节
            status.CPUID = CPUID;

            //U32 FAID;               //FAID
            string FAID = Formater.ReverseHexString(data.Substring(112, 2 * 4));//4字节
            status.FAID = FAID;

            return status;
        }

        public static Status FomartStatus(SStatus s)
        {
            Status status = new Status();
            List<string> temp_r_List = new List<string>();
            foreach (var temp_r in s.temp_r)
            {
                var r = (decimal.Parse(temp_r.ToString()) / 10).ToString("#0.0");
                if (decimal.Parse(r) < -30 || decimal.Parse(r) > 60)
                {
                    r = "-";
                }
                temp_r_List.Add(r);
            }
            status.temp_r_List = temp_r_List;

            string humi_r = s.humi_r[0].ToString(); //2字节
            if (decimal.Parse(humi_r) < 0 || decimal.Parse(humi_r) > 99)
            {
                humi_r = "-";
            }
            status.humi_r = humi_r;

            string nh3_r = s.nh3_r.ToString(); //2字节
            if (decimal.Parse(nh3_r) < 0 || decimal.Parse(nh3_r) > 1000)
            {
                nh3_r = "-";
            }
            status.nh3_r = nh3_r;

            string co2_r = s.co2_r.ToString(); //2字节
            if (decimal.Parse(co2_r) < 0 || decimal.Parse(co2_r) > 5000)
            {
                co2_r = "-";
            }
            status.co2_r = co2_r;

            string wind_r = s.wind_r.ToString(); //4字节
            status.wind_r = wind_r;

            string temp_avg = s.temp_avg.ToString(); //2字节
            temp_avg = (decimal.Parse(temp_avg) / 10).ToString("#0.0");
            if (decimal.Parse(temp_avg) < -30 || decimal.Parse(temp_avg) > 60)
            {
                temp_avg = "-";
            }
            status.temp_avg = temp_avg;

            string temp_target = s.temp_target.ToString(); //2字节
            status.temp_target = (decimal.Parse(temp_target) / 10).ToString("#0.0");

            string day = s.day.ToString();
            status.day = day;

            string posFan = s.posFan.ToString(); //1字节
            status.posFan = posFan;

            //U8 posDoor;             //风门位置
            string posDoor = s.posDoor.ToString(); //1字节
            status.posDoor = posDoor;

            //U8 modeTemp;             //温度控制模式 0-自设 1-曲线
            status.modeTemp = (s.modeTemp == 1 ? "曲线" : "自设");

            //U8 modeRun;              //运行模式 0-空舍 1-生产
            status.modeRun = (s.modeRun == 1 ? "生产" : "空舍");

            //U8 modeCtrl;             //控制模式 0-线性 1-比例 2-固定
            if (s.modeCtrl == 0)
            {
                status.modeCtrl = "线性";
            }
            else if (s.modeCtrl == 1)
            {
                status.modeCtrl = "比例";
            }
            else if (s.modeCtrl == 2)
            {
                status.modeCtrl = "固定";
            }


            //U8 modeDoor;            //风门模式 0=变频, 1=继电器, 3=无
            if (s.modeDoor == 0)
            {
                status.modeDoor = "变频";
            }
            else if (s.modeDoor == 1)
            {
                status.modeDoor = "继电器";
            }
            else if (s.modeDoor == 3)
            {
                status.modeDoor = "无";
            }

            //U32 alarmState;         //报警状态，每1bit 表示一种报警 ,该位为0表示无此报警
            string alarmState = s.alarmState.ToString();//4字节
            status.alarmState = alarmState;

            //U32 relayState;         //每种类型的继电器对应的开关状态 
            string relayState = s.relayState.ToString();//4字节
            status.relayState = relayState;

            //U8  version[16];        //版本号
            string version = Formater.ASCLLHexBytesToString(s.version);//4字节
            status.version = version;

            //U32 CPUID;              //CPUID
            string CPUID = s.CPUID.ToString();//4字节
            status.CPUID = CPUID;

            //U32 FAID;               //FAID
            string FAID = s.FAID.ToString();//4字节
            status.FAID = FAID;

            var newList = new List<byte>(s.QCCID);
            var qccidList = newList.ConvertAll<string>(x => Formater.DECtoHEX(x.ToString(),2));
            qccidList.Reverse();
            status.QCCID = string.Join("", qccidList);

            status.NH3CO2 = s.NH3CO2.ToString();

            //status.rev=
            status.wYestoday = ((decimal)s.wYestoday / 10000).ToString();
            status.wToday = ((decimal)s.wToday/10000).ToString();
            status.eYestoday = ((decimal)s.eYestoday/10).ToString();
            status.eToday = ((decimal)s.eToday/10).ToString();

            return status;
        }

        public static Params DecodeParams(SParams _params)
        {
            Params res = new Params();
            string value = string.Empty;

            //温度曲线
            for (int i = 0; i < _params.tempFig_Day.Length; i++)
            {
                res.tempFig_Day.Add(_params.tempFig_Day[i]);
            }
            for (int i = 0; i < _params.tempFig_Target_Day.Length; i++)
            {
                res.tempFig_Target_Day.Add(_params.tempFig_Target_Day[i] / 10);
            }
            for (int i = 0; i < _params.tempFig_Target_Night.Length; i++)
            {
                res.tempFig_Target_Night.Add(_params.tempFig_Target_Night[i] / 10);
            }
            for (int i = 0; i < _params.tempFig_dTempMax.Length; i++)
            {
                res.tempFig_dTempMax.Add(_params.tempFig_dTempMax[i] / 10);
            }
            for (int i = 0; i < _params.tempFig_WindMin.Length; i++)
            {
                res.tempFig_WindMin.Add((int)_params.tempFig_WindMin[i]);
            }
            for (int i = 0; i < _params.tempFig_WindMax.Length; i++)
            {
                res.tempFig_WindMax.Add((int)_params.tempFig_WindMax[i]);
            }
            //制冷--曲线
            for (int i = 0; i < _params.cool_Day.Length; i++)
            {
                res.cool_Day.Add(_params.cool_Day[i]);
            }
            for (int i = 0; i < _params.cool_STime.Length; i++)
            {
                res.cool_STime.Add(_params.cool_STime[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }
            for (int i = 0; i < _params.cool_ETime.Length; i++)
            {
                res.cool_ETime.Add(_params.cool_ETime[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }
            for (int i = 0; i < _params.cool_STemp.Length; i++)
            {
                res.cool_STemp.Add(_params.cool_STemp[i] / 10);
            }
            for (int i = 0; i < _params.cool_ETemp.Length; i++)
            {
                res.cool_ETemp.Add(_params.cool_ETemp[i] / 10);
            }
            for (int i = 0; i < _params.cool_EHumi.Length; i++)
            {
                res.cool_EHumi.Add(_params.cool_EHumi[i] / 10);
            }
            for (int i = 0; i < _params.cool_On.Length; i++)
            {
                res.cool_On.Add(_params.cool_On[i] / 10);
            }
            for (int i = 0; i < _params.cool_Off.Length; i++)
            {
                res.cool_Off.Add(_params.cool_Off[i] / 10);
            }
            //加热
            for (int i = 0; i < _params.heat_Day.Length; i++)
            {
                res.heat_Day.Add(_params.heat_Day[i]);
            }
            for (int i = 0; i < _params.heat_STime.Length; i++)
            {
                res.heat_STime.Add(_params.heat_STime[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }
            for (int i = 0; i < _params.heat_ETime.Length; i++)
            {
                res.heat_ETime.Add(_params.heat_ETime[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }
            for (int i = 0; i < _params.heat_STemp.Length; i++)
            {
                res.heat_STemp.Add(_params.heat_STemp[i] / 10);
            }
            for (int i = 0; i < _params.heat_ETemp.Length; i++)
            {
                res.heat_ETemp.Add(_params.heat_ETemp[i] / 10);
            }
            for (int i = 0; i < _params.heat_On.Length; i++)
            {
                res.heat_On.Add(_params.heat_On[i] / 10);
            }
            for (int i = 0; i < _params.heat_Off.Length; i++)
            {
                res.heat_Off.Add(_params.heat_Off[i] / 10);
            }
            for (int i = 0; i < _params.heat_Percent.Length; i++)
            {
                res.heat_Percent.Add(_params.heat_Percent[i]);
            }
            //定时输出
            for (int i = 0; i < _params.timer_On.Length; i++)
            {
                res.timer_On.Add(_params.timer_On[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }
            for (int i = 0; i < _params.timer_Off.Length; i++)
            {
                res.timer_Off.Add(_params.timer_Off[i].ToString().PadLeft(4, '0').Insert(2, ":"));
            }

            res.sys_NH3CO2 = _params.sys_NH3CO2;
            res.addr_Wmeter = _params.addr_Wmeter;
            res.addr_Emeter = _params.addr_Emeter;

            //模式控制
            res.sys_RunMode = _params.sys_RunMode;
            res.sys_TempMode = _params.sys_TempMode;
            res.sys_WindMode = _params.sys_WindMode;
            res.sys_DoorMode = _params.sys_DoorMode;
            //系统变量
            res.sys_FanMin = _params.sys_FanMin;
            res.sys_DoorMin = _params.sys_DoorMin;
            res.sys_WindStep = _params.sys_WindStep;
            res.sys_DoorStep = _params.sys_DoorStep;
            res.sys_WindTime = _params.sys_WindTime;
            res.sys_HumiAdd = _params.sys_HumiAdd;
            res.sys_HumiHigh = _params.sys_HumiHigh / 10;
            res.sys_Humi_Delay = _params.sys_Humi_Delay / 10;
            res.sys_NH3Add = _params.sys_NH3Add;
            res.sys_NH3High = _params.sys_NH3High;
            res.sys_NH3_Delay = _params.sys_NH3_Delay / 10;
            res.sys_CO2Add = _params.sys_CO2Add;
            res.sys_CO2High = _params.sys_CO2High;
            res.sys_CO2_Delay = _params.sys_CO2_Delay / 10;
            res.sys_DayStart = _params.sys_DayStart.ToString().PadLeft(4, '0').Insert(2, ":");
            res.sys_DayEnd = _params.sys_DayEnd.ToString().PadLeft(4, '0').Insert(2, ":");
            //报警参数
            res.alarm_DoorTO = _params.alarm_DoorTO;
            res.alarm_TempMax = _params.alarm_TempMax / 10;
            res.alarm_TempMin = _params.alarm_TempMin / 10;
            res.alarm_TempDMax = _params.alarm_TempDMax / 10;
            res.alarm_TempDMin = _params.alarm_TempDMin / 10;
            res.alarm_HumiMax = _params.alarm_HumiMax / 10;
            res.alarm_HumiMin = _params.alarm_HumiMin / 10;
            res.alarm_CO2Max = _params.alarm_CO2Max;
            res.alarm_NH3Max = _params.alarm_NH3Max;
            res.alarm_WindMin = (int)_params.alarm_WindMin;
            for (int i = 0; i < _params.alarm_TempSensor.Length; i++)
            {
                res.alarm_TempSensor.Add(_params.alarm_TempSensor[i]);
            }
            for (int i = 0; i < _params.alarm_HumiSensor.Length; i++)
            {
                res.alarm_HumiSensor.Add(_params.alarm_HumiSensor[i]);
            }
            res.alarm_NH3Sensor = _params.alarm_NH3Sensor;
            res.alarm_CO2Sensor = _params.alarm_CO2Sensor;
            res.alarm_LostPower = _params.alarm_LostPower;
            res.sys_fixMaxWind = (int)_params.sys_fixMaxWind;


            return res;
        }
    }
}
